<template>
  <div class="channel-container">
    <div class="channel-row">
      <div class="channel-item" v-for="item in firstRow" :key="item.id" @click="Gotogoodslist(item.id)">
        <img :src="item.iconUrl" class="channel-icon" />
        <span class="channel-text">{{ item.name }}</span>
      </div>
    </div>

    <div class="channel-row" v-if="secondRow.length > 0">
      <div class="channel-item" v-for="item in secondRow" :key="item.id" @click="Gotogoodslist(item.id)" >
        <img :src =" item.iconUrl" class="channel-icon" />
      <span class="channel-text">{{ item.name }}</span>
    </div>
    <div class="channel-item placeholder" v-for="i in 5 - secondRow.length" :key="'placeholder-' + i"></div>
  </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { getHomeData } from '@/api/home'
import { useRouter } from 'vue-router'
const channel = ref([])
const router = useRouter()
const firstRow = computed(() => channel.value.slice(0, 5))
const secondRow = computed(() => channel.value.slice(5))

onMounted(async () => {
  const response = await getHomeData()
  channel.value = response.data.channel
})
const Gotogoodslist = (id) => {
  router.push({
    path: "/goods/list",
    query: { id }
  })
}
</script>

<style scoped>
.channel-container {
  padding: 20px 0;
  background: #fff;
}

.channel-row {
  display: flex;
  justify-content: space-between;
  padding: 0 15px;
  margin-bottom: 15px;
}

.channel-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: calc(20% - 10px);
  min-width: 0;
}

.channel-icon {
  width: 48px;
  height: 48px;
  margin-bottom: 10px;
  filter: brightness(0.9);
}

.channel-text {
  font-size: 14px;
  font-weight: 500;
  color: #333;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
}

.placeholder {
  visibility: hidden;
}
</style>